using System.Collections.Generic;
using ProtoModels;
using UnityEngine;

public class PlayerCareer : SingletonInit<PlayerCareer>
{
	public class StageProgress
	{
		public CareerProgressDAO.Stage stage;

		public bool isLost
		{
			get
			{
				return stage.wonGames < stage.playedGames;
			}
		}

		public StageProgress(CareerProgressDAO.Stage stage)
		{
			this.stage = stage;
		}

		public bool IsWon(int substageCount)
		{
			return stage.wonGames >= substageCount;
		}

		public bool IsMaxWon(int index)
		{
			return stage.maxWonGames == index;
		}

		public bool IsActive(int index)
		{
			return stage.wonGames == index;
		}

		public bool IsPassed(int index)
		{
			return stage.wonGames > index;
		}

		public bool IsLocked(int index)
		{
			return stage.wonGames < index;
		}

		public void Reset()
		{
			stage.playedGames = 0;
			stage.wonGames = 0;
			if (stage.subStages != null)
			{
				foreach (CareerProgressDAO.SubStage subStage in stage.subStages)
				{
					subStage.playerScore = 0;
				}
			}
			SingletonInit<PlayerCareer>.instance.Save();
		}

		public void MakeSane(PlayerCareerStageDefinition.StageDefinition stageDefinition = null)
		{
			if (stage.playedGames > stage.wonGames)
			{
				Reset();
			}
			if (stage.playedGames < stage.wonGames)
			{
				stage.playedGames = stage.wonGames;
			}
			if (stageDefinition != null && stageDefinition.subStages != null && stageDefinition.subStages.Count > 0)
			{
				stage.playedGames = Mathf.Min(stage.playedGames, stageDefinition.subStages.Count - 1);
				stage.wonGames = Mathf.Min(stage.wonGames, stageDefinition.subStages.Count - 1);
			}
			SingletonInit<PlayerCareer>.instance.Save();
		}

		public CareerProgressDAO.SubStage GetSubStageProgress(int index)
		{
			if (stage.subStages == null)
			{
				stage.subStages = new List<CareerProgressDAO.SubStage>();
			}
			int count = stage.subStages.Count;
			for (int i = count; i <= index; i++)
			{
				stage.subStages.Add(new CareerProgressDAO.SubStage());
			}
			return stage.subStages[index];
		}
	}

	public CareerScoreDAO scoreDAO;

	public CareerProgressDAO progressDAO;

	protected string scoreFilename = "cs.bytes";

	protected string progressFilename = "pg.bytes";

	public string careerProgressFilename
	{
		get
		{
			return scoreFilename;
		}
	}

	public int maxStageReached
	{
		get
		{
			return scoreDAO.maxStageReached;
		}
	}

	public CareerScoreDAO.StageScore GetStageScore(string name)
	{
		if (scoreDAO.stageScores == null)
		{
			scoreDAO.stageScores = new List<CareerScoreDAO.StageScore>();
		}
		foreach (CareerScoreDAO.StageScore stageScore2 in scoreDAO.stageScores)
		{
			if (stageScore2.name == name)
			{
				return stageScore2;
			}
		}
		CareerScoreDAO.StageScore stageScore = new CareerScoreDAO.StageScore();
		stageScore.name = name;
		scoreDAO.stageScores.Add(stageScore);
		return stageScore;
	}

	public StageProgress GetStageProgress(string name)
	{
		if (progressDAO.stageProgress == null)
		{
			progressDAO.stageProgress = new List<CareerProgressDAO.Stage>();
		}
		foreach (CareerProgressDAO.Stage item in progressDAO.stageProgress)
		{
			if (item.name == name)
			{
				return new StageProgress(item);
			}
		}
		CareerProgressDAO.Stage stage = new CareerProgressDAO.Stage();
		stage.name = name;
		progressDAO.stageProgress.Add(stage);
		return new StageProgress(stage);
	}

	public int TotalNumberOfStars()
	{
		int num = 0;
		if (scoreDAO == null || scoreDAO.stageScores == null)
		{
			return num;
		}
		foreach (CareerScoreDAO.StageScore stageScore in scoreDAO.stageScores)
		{
			num += stageScore.stars;
		}
		return num;
	}

	public override void Init()
	{
		ReloadModels();
	}

	public void ReloadModels()
	{
		if (!ProtoIO.LoadFromFile<ProtoSerializer, CareerScoreDAO>(scoreFilename, GGFileIO.instance, out scoreDAO))
		{
			scoreDAO = new CareerScoreDAO();
		}
		if (!ProtoIO.LoadFromFile<ProtoSerializer, CareerProgressDAO>(progressFilename, GGFileIO.instance, out progressDAO))
		{
			progressDAO = new CareerProgressDAO();
		}
	}

	public void Save()
	{
		if (scoreDAO != null)
		{
			ProtoIO.SaveToFileCloudSync(scoreFilename, scoreDAO);
		}
		if (progressDAO != null)
		{
			ProtoIO.SaveToFileCloudSync(progressFilename, progressDAO);
		}
	}
}
